/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.comut.crud.ejb;

import com.comut.crud.bo.SubgroupHandler;
import com.comut.crud.connection.MooveFleetConnectionManager;
import com.comut.crud.ejb.interfaces.local.ISubgroupActionLocal;
import com.comut.crud.ejb.interfaces.remote.ISubgroupActionRemote;
import com.comut.crud.pojo.subgroup.Subgroup;
import com.comut.lib.error.ErrorCodes;
import com.comut.lib.exception.BaseException;
import java.sql.Connection;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Stateless;
import org.apache.log4j.Logger;

/**
 *
 * @author asantos
 */
@Stateless
public class SubgroupBean implements ISubgroupActionLocal, ISubgroupActionRemote {

    private static Logger _logger = Logger.getLogger(SubgroupBean.class.getName());

    @PostConstruct
    private void subgroupPostConstruct() {
        _logger.info("PostConstruct");
    }

    @PreDestroy
    private void subgroupPreDestroy() {
        _logger.info("PreDestroy");
    }

    public SubgroupBean() {
    }

    public Subgroup insertSubgroup(Subgroup subgroup) {
        Connection connection = MooveFleetConnectionManager.instance().getConnection();
        SubgroupHandler th;
        Subgroup toRet;
        try {
            th = new SubgroupHandler(connection);
            toRet = th.insertSubgroup(subgroup);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
            th = null;
        }
        return toRet;
    }

    public void deleteSubgroup(Subgroup subgroup) {
        if(subgroup.getId()<1)
            throw new BaseException("The id must be a int bigger than 0", ErrorCodes.INVALID_ID);
        Connection connection = MooveFleetConnectionManager.instance().getConnection();
        SubgroupHandler th;
        try {
            th = new SubgroupHandler(connection);
            th.deleteSubgroup(subgroup);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
            th = null;
        }
    }

    public void updateSubgroup(String subgroup) {
        Connection connection = MooveFleetConnectionManager.instance().getConnection();
        SubgroupHandler th;
        try {
            th = new SubgroupHandler(connection);
            th.updateSubgroup(subgroup);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
            th = null;
        }
    }

    public Subgroup getSubgroup(Subgroup subgroup) {
        Connection connection = MooveFleetConnectionManager.instance().getConnection();
        Subgroup toRet;
        SubgroupHandler th;
        try {
            th = new SubgroupHandler(connection);
            toRet = th.getSubgroup(subgroup);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
            th = null;
        }
        return toRet;
    }
}
